package org.lora.core.context;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.lora.core.configxml.entity.ConfigXmlEntity;
import org.lora.mvc.entity.ActionMethodEntity;
import org.lora.mvc.security.context.SecurityContext;
import org.lora.quartz.IQuartzService;

/**
 * <p>
 * Title:WebContext
 * </p>
 * <p>
 * Desription:Web上下文 单例
 * </p>
 * Create Time:2016年7月5日 下午6:52:36
 * 
 * @author Bladnir@outlook.com
 *         <p>
 *         History:
 *         </p>
 *         2016年7月5日 Bladnir Create</br>
 *         2016年11月8日 Bladnir 添加dataSourceMap</br>
 *         2016年11月8日 Bladnir 添加quartzService</br>
 */
public class WebContext {

	private static WebContext webContext;

	private Map<String, ActionMethodEntity> actionMap = new HashMap<>();

	private Map<String, SqlSessionFactory> sqlSessionFactoryMap = new HashMap<>();

	private ConfigXmlEntity configXmlEntity;

	private SecurityContext securityContext;
	
	private IQuartzService quartzService;

	/**
	 * BoneCP 数据源Map key：environmentName
	 */
	private Map<String, DataSource> dataSourceMap = new HashMap<>();

	/**
	 * key:service接口类全名 value:装配好的实现对象
	 */
	private Map<String, Object> serviceBeanMap = new HashMap<>();

	public static WebContext getInstance() {
		if (webContext == null) {
			webContext = new WebContext();
		}

		return webContext;
	}

	public ConfigXmlEntity getConfigXmlEntity() {
		return configXmlEntity;
	}

	public void setConfigXmlEntity(ConfigXmlEntity configXmlEntity) {
		this.configXmlEntity = configXmlEntity;
	}

	/**
	 * 添加ActionMap
	 * 
	 * @param actionMap
	 */
	public void addActionMap(Map<String, ActionMethodEntity> actionMap) {
		this.actionMap = actionMap;
	}

	public Map<String, ActionMethodEntity> getActionMap() {
		return this.actionMap;
	}

	/**
	 * 获取ActionBean
	 * 
	 * @param path
	 * @return
	 */
	public ActionMethodEntity getActionBean(String path) {

		if (this.actionMap.containsKey(path)) {
			return actionMap.get(path);
		}
		return null;
	}

	public void setSqlSessionFactoryMap(Map<String, SqlSessionFactory> sqlSessionFactoryMap) {
		this.sqlSessionFactoryMap = sqlSessionFactoryMap;
	}

	public Map<String, SqlSessionFactory> getSqlSessionFactoryMap() {
		return this.sqlSessionFactoryMap;
	}

	public Map<String, Object> getServiceBeanMap() {
		return serviceBeanMap;
	}

	public void setServiceBeanMap(Map<String, Object> serviceBeanMap) {
		this.serviceBeanMap = serviceBeanMap;
	}

	public SecurityContext getSecurityContext() {
		return securityContext;
	}

	public void setSecurityContext(SecurityContext securityContext) {
		this.securityContext = securityContext;
	}

	public Map<String, DataSource> getDataSourceMap() {
		return dataSourceMap;
	}

	public void setDataSourceMap(Map<String, DataSource> dataSourceMap) {
		this.dataSourceMap = dataSourceMap;
	}

	public IQuartzService getQuartzService() {
		return quartzService;
	}

	public void setQuartzService(IQuartzService quartzService) {
		this.quartzService = quartzService;
	}
	
	

}
